package com.zxmzy.scaffold.project

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.youth.banner.adapter.BannerAdapter
import com.youth.banner.config.BannerConfig
import com.zxmzy.scaffold.core.base.BaseMvvmActivity
import com.zxmzy.scaffold.http.RequestManager
import com.zxmzy.scaffold.project.databinding.ActivityMainBinding
import com.zxmzy.scaffold.project.databinding.ItemBannerBinding

class MainActivity : BaseMvvmActivity<ActivityMainBinding, MainViewModel>() {
    private val adapter = BannerAdapter()
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.banner.setAdapter(adapter)
        RequestManager.get().resetBaseUrl("https://www.wanandroid.com")
        mViewModel.loadingLiveData.observe(this) {
            if (it) {
                loadingManager.showLoading()
            } else {
                loadingManager.dismissLoading()
            }
        }
//        mViewModel.login().observe(this){
//
//        }
        mViewModel.bannerLiveData.observe(this) { list ->
            list?.let {
                adapter.setBanner(it)
                mBinding.banner.start()
            }
        }
        mViewModel.getBanner()
    }

}

class BannerAdapter :
    BannerAdapter<Banner, com.zxmzy.scaffold.project.BannerAdapter.VH>(mutableListOf()) {
    class VH(val binding: ItemBannerBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateHolder(parent: ViewGroup, viewType: Int): VH {
       val binding:ItemBannerBinding = ItemBannerBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return VH(binding)
    }

    override fun onBindView(holder: VH, data: Banner, position: Int, size: Int) {
        Glide.with(holder.binding.ivImage).load(data.imagePath).into(holder.binding.ivImage)
    }

    fun setBanner(list:List<Banner>){
        mDatas.clear()
        mDatas.addAll(list)
        notifyDataSetChanged()
    }
}